var table = null;
var pid;
layui
    .config({
        base: '../layui/exts/', //静态资源所在路径
    })
    .use(function () {
        var form = layui.form;
        $ = layui.$;
        var util = layui.util;

        pid = $("#projectList").val();
        openTask();

        form.render();
        form.on("select(projectList)", function (data) {
            pid = data.value;
            openTask();
        });

        function openTask() {
            table = layui.table;
            table.render({
                elem: '#dataTable'
                , url: '/project/getTaskInfo?pid=' + pid
                , method: 'get'
                , cellMinWidth: 50
                , page: true
                , limit: 50
                , height: 'full-65'
                , loading: true
                , size: 'sm'
                , cols: [
                    [ //标题栏
                        {field: 'status', title: '状态', fixed: 'left', width: 50, templet: "#statusTpl"},
                        {field: 'taskName', title: '任务名称', width: 300, fixed: 'left'},
                        {field: 'taskModel', title: '任务类型', width: 70, templet: "#taskmodelTpl"},
                        {field: 'archivesClassify', title: '档案门类', width: 150},
                        {field: 'fillingModel', title: '归档方式', width: 100, templet: "#fillingModelTpl"},
                        {field: 'psStartDate', title: '预计启动日期', width: 85},
                        {field: 'psEndDate', title: '预计结束日期', width: 85},
                        {field: 'startDate', title: '实际启动日期', width: 85},
                        {field: 'endDate', title: '实际结束日期', width: 85},
                        {field: 'remark', title: '备注', width: 120},
                        {fixed: 'right', title: '操作', width: 140, toolbar: '#operationBar'}
                    ]
                ],
            });
        }

        util.on({
            newTask: function () {
                if (pid === '' || pid == undefined) {
                    top.parent.layer.alert("请选择需要创建任务的项目", {icon: 2});
                    return false;
                }
                top.layer.open({
                    type: 2,
                    title: '新增任务',
                    content: './project/setTaskInfo?id=&pid=' + pid,
                    area: ['700px', '590px'],
                    shadeClose: false,
                    resize: false,
                    btn: [
                        '<i class="layui-icon">&#xe605;</i> 确定',
                        '<i class="layui-icon">&#x1006;</i> 取消',
                    ],
                    yes: function (index, layero) {
                        let iframeWin = layero.find('iframe')[0];
                        if (iframeWin.contentWindow.Submit()) {
                            table.reload('dataTable');
                        }
                    },
                    btn2: function (index) {
                        layer.close(index);
                    },
                });
                return false;
            }
        });

        //监听工具条
        table.on('tool(dataTable)', function (obj) {
            var data = obj.data;
            switch (obj.event) {
                case "edit":
                    top.layer.open({
                        type: 2,
                        title: '编辑项目信息',
                        content: './project/setTaskInfo?id=' + data.id + '&pid=' + pid,
                        area: ['700px', '590px'],
                        shadeClose: false,
                        resize: false,
                        btn: [
                            '<i class="layui-icon">&#xe605;</i> 确定',
                            '<i class="layui-icon">&#x1006;</i> 取消',
                        ],
                        yes: function (index, layero) {
                            var iframeWin = layero.find('iframe')[0];
                            if (iframeWin.contentWindow.Submit()) {
                                table.reload('dataTable');
                            }
                        },
                        btn2: function (index, layero) {
                            layer.close(index); //如果设定了yes回调，需进行手工关闭
                        },
                    });
                    return false;
                case 'del':
                    top.layer.confirm('确定要删除当前选择的任务信息吗？ ', function (index) {
                        $.ajax({
                            url: '../project/delTaskInfo', //url
                            dataType: 'json', //数据格式
                            type: 'POST', //请求方式
                            async: false, //是否异步请求
                            data: {id: data.id},
                            success: function (result) {
                                if (result.code == 0) {
                                    obj.del();
                                    layer.close(index);
                                    top.parent.layer.msg(result.message, {icon: 1});
                                } else {
                                    top.parent.layer.alert(result.message, {icon: 2});
                                }
                            },
                        });
                    });
                    return false;
                case 'start':
                    top.layer.open({
                        type: 2,
                        title: '任务启动',
                        content: './project/setTaskStart?id=' + data.id,
                        area: ['500px', '550px'],
                        shadeClose: false,
                        resize: false,
                        btn: [
                            '<i class="layui-icon">&#xe605;</i> 确定',
                            '<i class="layui-icon">&#x1006;</i> 取消',
                        ],
                        yes: function (index, layero) {
                            var body = layer.getChildFrame('body', index);
                            var iframeWin = layero.find('iframe')[0];
                            if (iframeWin.contentWindow.Submit()) {
                                table.reload('dataTable');
                            }
                        },
                        btn2: function (index, layero) {
                            layer.close(index); //如果设定了yes回调，需进行手工关闭
                        },
                    });
                    return false;
                case 'finish':
                    top.layer.open({
                        type: 2,
                        title: '任务结束',
                        content: './project/setTaskFinish?id=' + data.id,
                        area: ['500px', '550px'],
                        shadeClose: false,
                        resize: false,
                        btn: [
                            '<i class="layui-icon">&#xe605;</i> 确定',
                            '<i class="layui-icon">&#x1006;</i> 取消',
                        ],
                        yes: function (index, layero) {
                            var body = layer.getChildFrame('body', index);
                            var iframeWin = layero.find('iframe')[0];
                            if (iframeWin.contentWindow.Submit()) {
                                table.reload('dataTable');
                            }
                        },
                        btn2: function (index, layero) {
                            layer.close(index); //如果设定了yes回调，需进行手工关闭
                        },
                    });
                    return false;
                case 'taskWork':
                    top.layer.open({
                        type: 2,
                        title: '任务流程序选择',
                        content: './project/setTaskWorkProcedure?id=' + data.id + '&pid=' + pid,
                        area: ['600px', '590px'],
                        shadeClose: false,
                        resize: false
                    });
                    return false;
            }
        });

    });
